package com.ffdms.mobileoffice;

import java.util.List;

import com.ffdms.service.MessageDigest;
import com.ffdms.util.MobileOfficeUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MessageAdapter extends BaseAdapter {

	private Context context;
	private List<MessageDigest> messages;

	public MessageAdapter(Context context, List<MessageDigest> messages) {
		this.context = context;
		this.messages = messages;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return messages.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return messages.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.message_item_view, null);
			holder = new ViewHolder();
			holder.tv_msg_title = (TextView) convertView
					.findViewById(R.id.tv_msg_title);
			holder.tv_msg_time = (TextView) convertView
					.findViewById(R.id.tv_msg_time);
			holder.tv_msg_content = (TextView) convertView
					.findViewById(R.id.tv_msg_content);
			holder.iv_msg_photo = (ImageView) convertView
					.findViewById(R.id.iv_msg_photo);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (messages != null) {
			MessageDigest md = messages.get(position);
			holder.tv_msg_title.setText(md.getTitle());
			String d = MobileOfficeUtil.formatDate("MM月dd日", md.getTime());
			holder.tv_msg_time.setText(d);
			holder.tv_msg_content.setText(md.getContent());
			holder.iv_msg_photo.setImageDrawable(md.getPhoto());
		}
		return convertView;
	}

	static class ViewHolder {
		ImageView iv_msg_photo;
		TextView tv_msg_title;
		TextView tv_msg_time;
		TextView tv_msg_content;
	}
}
